package cc.angis.xiugallery.db;

import java.util.ArrayList;
import java.util.List;

import cc.angis.xiugallery.data.PlatFormData;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

public class PostPlatformDBHelper extends BaseDBHelper {
	
	public PostPlatformDBHelper(){
		
	}
	
	public PostPlatformDBHelper(Context context, String table){
		super(context, table);
	}
	
	public PostPlatformDBHelper(Context context, String table, ContentValues values){
		super(context, table, values);
	}
	
	public long delete(String postid){
		mWhereClaus = "postid=?";
		mWhereArgs = new String[] { String.valueOf(postid) };
		return delDB();
	}
	
	public long insert(String postid, String platformid, int platformtype){
			mValues = new ContentValues();
			mValues.put(TableConstants.PostPlatFormColumn.POSTID, postid);
			mValues.put(TableConstants.PostPlatFormColumn.PLATFORMID, platformid);
			mValues.put(TableConstants.PostPlatFormColumn.PLATFORMTYPE, platformtype);
//			lContentValues.put(TableConstants.FavouriteImagesColumn.FAVNAME, favname);
			return insertDB();
	}
	
	public List<PlatFormData> getAll(){
		List<PlatFormData> platformDatas = new ArrayList<PlatFormData>();
		Cursor cursor = mDBHelper.getReadableDatabase().query(mTable, null, 
				null, null, null, null, null);
		int postIdColumn = cursor.getColumnIndex(TableConstants.PostPlatFormColumn.POSTID);
		int platformIdColumn = cursor.getColumnIndex(
				TableConstants.PostPlatFormColumn.PLATFORMID);
		int platformTypeColumn = cursor.getColumnIndex(
				TableConstants.PostPlatFormColumn.PLATFORMTYPE);
		if (cursor.moveToFirst()){
			PlatFormData data;
			do {
				data = new PlatFormData();
				data.setPostId(cursor.getString(postIdColumn));
				data.setPlatformId(cursor.getString(platformIdColumn));
				data.setPlatformType(cursor.getInt(platformTypeColumn));
				platformDatas.add(data);
			}while(cursor.moveToNext());
		}
		cursor.close();
		return platformDatas;
	}
	
	public List<PlatFormData> getAllByPostId(String postid){
		List<PlatFormData> platformDatas = new ArrayList<PlatFormData>();
		Cursor cursor = mDBHelper.getReadableDatabase().query(mTable, new String[]{TableConstants.PostPlatFormColumn.PLATFORMID, TableConstants.PostPlatFormColumn.PLATFORMTYPE}, 
				"postid=?", new String[]{ postid}, null, null, null);
		int platformIdColumn = cursor.getColumnIndex(
				TableConstants.PostPlatFormColumn.PLATFORMID);
		int platformTypeColumn = cursor.getColumnIndex(
				TableConstants.PostPlatFormColumn.PLATFORMTYPE);
		if (cursor.moveToFirst()){
			PlatFormData data;
			do {
				data = new PlatFormData();
				data.setPlatformId(cursor.getString(platformIdColumn));
				data.setPlatformType(cursor.getInt(platformTypeColumn));
				platformDatas.add(data);
			}while(cursor.moveToNext());
		}
		cursor.close();
		return platformDatas;
	}
	
}
